package logic

import (
	"context"
	"jdshopuser/user_srv/user"

	"jdshopuser/api/internal/svc"
	"jdshopuser/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateAvatarLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateAvatarLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAvatarLogic {
	return &UpdateAvatarLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateAvatarLogic) UpdateAvatar(req *types.UpdateAvatarRequest) (resp *types.RegMsgResponse, err error) {
	// todo: 修改用户头像

	//获取验证码
	code, err := l.svcCtx.UserSrv.GetSmsCode(l.ctx, &user.GetSmsCodeRequest{
		Mobile: req.Mobile,
		Sale:   "update",
	})
	if err != nil {
		return &types.RegMsgResponse{
			Cre:   -1,
			Msg:   "服务器错误",
			Datra: nil,
		}, err
	}
	//判断验证码
	if code.Code != req.Code {
		return &types.RegMsgResponse{
			Cre:   -2,
			Msg:   "验证码错误",
			Datra: nil,
		}, err
	}

	//修改
	_, err = l.svcCtx.UserSrv.UpdateAvatar(l.ctx, &user.UpdateAvatarRequest{
		Mobile: req.Mobile,
		Avatar: req.Avatar,
	})
	if err != nil {
		return &types.RegMsgResponse{
			Cre:   -1,
			Msg:   "服务器错误",
			Datra: nil,
		}, err
	}

	return &types.RegMsgResponse{
		Cre:   0,
		Msg:   "图片更新成功",
		Datra: nil,
	}, err
}
